<?php

function parse_platform( $batch, $platform ) {
    $context = getContext('GET');
    echo $context;

    $baseurl = "https://www.invesdor.com";
    $url = 'https://www.invesdor.com/en/pitches/all';
    $html = file_get_html($url, false, $context);
    
    $articles = $html->find('div[class*="pitch-accessible"]');

    foreach($articles as $article) {
        $href = $article->find('a[class="read-more"]',0)->href;
        $link = $baseurl . $href;
        $name = trim($article->find('div[class="title"]',0)->plaintext);
        $description = str_replace('Read more','',$article->find('div[class="teaser"]',0)->plaintext);
        $external_id = substr($href,strrpos($href,'/')+1);
        $currency= '€';
        
        $tmptxt = $article->find('div[class="image"]',0)->style;
        $image = $baseurl . str_replace('preview_normal','main',str_replace(')','',str_replace('background-image: url(','',$tmptxt)));
        
        $tmptxt = $article->find('div[class="deadline"]',0)->plaintext;
        if(strpos($tmptxt,'closed') > 0)
            $days = 0;
        else 
            $days = substr($tmptxt,0,strpos($tmptxt,' '));
        
        $tmptxt = $article->find('div[class="target"]',0)->plaintext;
        $target = str_replace('\r','',str_replace($currency,'',substr($tmptxt,strpos($tmptxt,'-')+1)));
        
        $tmptxt = $article->find('div[class="progress-text"]',0)->innertext;
        $current = str_replace('&nbsp;','',$tmptxt );
            
//        if(strpos($tmptxt,'Round') >= 0)
//            $current = 'test';//str_replace('secured','',str_replace('invested','',str_replace($currency,'',str_replace(chr(160),'',str_replace('Round','',$tmptxt)))));
//        else 
//            $current = str_replace($currency,'',str_replace(chr(160),'',str_replace('until round is secured','',$tmptxt)));
        
        $shares = '0';
        $worth = '0';
        $active = $days > 0 ? 1 : 0;
        
        echo $tmptxt.'-'.$current.'<br/>';
        
//        if($batch) {
//            saveProjectData($external_id, $platform, $batch, $current, $days, $worth, $shares, $target, $image, $link, $name, $description, $active, $currency);
//        }
    }
}
?>